﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using MFTool;
using UIBase;

namespace AutoUI.Areas.ConfigUI.EasyUICtrl
{
    public class ComboBox : CtrlBase
    {
        public ComboBox(string id, EasyUICtrlPrepareData pData)
            : base(id, pData)
        {
            if (_dataOption.Count == 0)
            {
                _dataOption = DefaultValue.ComboBox().JsonToDictionary();
            }
        }

        public override void Prepare()
        {
            DomStr = "select";
            _class.Add("easyui-combobox");
            string sourceType = _dataOption.GetValue("SourceType");
            if (sourceType == "MF_Enum")
            {
                string dataJson = CommonHelper.GetDBEnum(_dataOption.GetValue("SourceValue")).ToJson();
                _dataOption.SetValue("data", dataJson);
            }
            else
            {
                _dataOption.SetValue("data", _dataOption.GetValue("SourceValue"));
            }
            _dataOption.Remove("SourceType");
            _dataOption.Remove("SourceValue");
        }

        public override void PrepareSearchCtrl()
        {
            Prepare();
            _style.SetValue("width", "100%");
        }

        protected override void AfterDataOptionToJson(ref string dataOption)
        {
            dataOption = dataOption.Replace("\"[", "[").Replace("]\"", "]").Replace("\\", "");
        }

        public override string GetScript()
        {
            return "";
        }

        protected override Dictionary<string, object> GetGridOption(string fieldName)
        {
            var tmpDataOption = new Dictionary<string, object>(_dataOption);
            string sourceType = tmpDataOption.GetValue("SourceType");
            if (sourceType == "MF_Enum")
            {
                string dataJson = CommonHelper.GetDBEnum(tmpDataOption.GetValue("SourceValue")).ToJson();
                dataJson = dataJson.Replace("\"", "'");
                tmpDataOption.SetValue("data", "!-" + dataJson + "-!");
            }
            else
            {
                tmpDataOption.SetValue("data", "!-" + _dataOption.GetValue("SourceValue") + "-!");
            }
            tmpDataOption.Remove("SourceType");
            tmpDataOption.Remove("SourceValue");

            return tmpDataOption;
        }

        public override string GetGridScript(string fieldName, string gridId)
        {
            string script = "";
            string sourceType = _dataOption.GetValue("SourceType");
            string sourceValue = "[]";
            if (sourceType == "MF_Enum")
            {
                sourceValue = CommonHelper.GetDBEnum(_dataOption.GetValue("SourceValue")).ToJson();
            }
            else
            {
                sourceValue = _dataOption.GetValue("SourceValue");
            }
            script += "var {0}EnumData = {1};\r\n".ReplaceArg(fieldName, sourceValue);
            script += "addGridEnum('{0}','{1}','{1}EnumData');".ReplaceArg(gridId, fieldName);

            return script;
        }
    }
}